<html>
<head><meta charset="utf-8"><title>repr pointers and references · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html">repr pointers and references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="151625178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625178">(Dec 13 2018 at 17:42)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span>  to me the current notation for <code>Slice&lt;T&gt;</code> and the trait objects aren't very human readable</p>



<a name="151625203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625203">(Dec 13 2018 at 17:42)</a>:</h4>
<p>When I see <code>Slice&lt;'a, T&gt; { ptr: &amp;'a T, len: usize }</code> I  wonder: "Does the pointer need to be valid?", "Does T matter?", "Does <code>'a</code> matter?", "Does any of this changes from <code>&amp;[T]</code> to <code>*[T]</code>?"etc.</p>



<a name="151625220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625220">(Dec 13 2018 at 17:43)</a>:</h4>
<p>But none of these questions are relevant for "representation".</p>



<a name="151625268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625268">(Dec 13 2018 at 17:43)</a>:</h4>
<p>I admit to being distracted by the use of "safe types" as well</p>



<a name="151625325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625325">(Dec 13 2018 at 17:44)</a>:</h4>
<p>Something like:</p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Slice</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="n">len</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">DynObject</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">data</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="n">vtable</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>does not raise these questions for me.</p>



<a name="151625896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625896">(Dec 13 2018 at 17:53)</a>:</h4>
<p>I tried writing it using <code>*T</code>, but I found that difficult to read, due to having to specify <code>*mut</code> or <code>*const</code>.</p>



<a name="151625930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151625930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151625930">(Dec 13 2018 at 17:53)</a>:</h4>
<p>I ended up using <code>u8</code> rather than <code>()</code> just to avoid ZSTs too.</p>



<a name="151626076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626076">(Dec 13 2018 at 17:55)</a>:</h4>
<p>it seems like a minor point we can revisit -- maybe it's ok just to say "pair of a pointer and length", roughly like so</p>



<a name="151626130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626130">(Dec 13 2018 at 17:56)</a>:</h4>
<p>I feel like once we have some idea what the "invariants" are we'll be in a better position to revisit this</p>



<a name="151626131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626131">(Dec 13 2018 at 17:56)</a>:</h4>
<p>I agree with you that the lifetimes are confusing though.</p>



<a name="151626155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626155">(Dec 13 2018 at 17:56)</a>:</h4>
<p>is the alignment of pointers the same as <code>usize</code> ?</p>



<a name="151626218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626218">(Dec 13 2018 at 17:57)</a>:</h4>
<p>if so, maybe we can just <code>usize</code> instead of pointers - I agree that the mutability of the pointer is irrelevant too, and that the <code>()</code> ZST type can be confusing as well =/</p>



<a name="151626423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626423">(Dec 13 2018 at 18:00)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I think it is, but then that's also confusing, as "morally" ptr is a pointer, not an integer. Sigh.</p>



<a name="151626462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626462">(Dec 13 2018 at 18:00)</a>:</h4>
<p>How about we use <code>*T</code> and put in some words to the "terminology" that we elide <code>mut</code> and <code>const</code> where they don't matter?</p>



<a name="151626490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626490">(Dec 13 2018 at 18:01)</a>:</h4>
<p>I don't really mind strongly enough about this, we can probably amend this later anyways, or when wording the final RFC. Do what you think its best</p>



<a name="151626500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626500">(Dec 13 2018 at 18:01)</a>:</h4>
<p>Will do!</p>



<a name="151626807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%20pointers%20and%20references/near/151626807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr.20pointers.20and.20references.html#151626807">(Dec 13 2018 at 18:05)</a>:</h4>
<p>Done.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>